package tech.daoxuan.findresources.controller;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tech.daoxuan.findresources.utils.JsoupUtils;
import tech.daoxuan.findresources.utils.RedisUtil;

import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName PutInfoToRedisController
 * @Description TODO
 * @Author daoxuan
 * @Date 2019/6/11 17:45
 * @Version 1.0
 */
@RestController
@RequestMapping("/content")
public class PutInfoToRedisController {
    Logger logger = LoggerFactory.getLogger(PutInfoToRedisController.class);

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("/{url}")
    public void getContentByUrl(@PathVariable String url) throws Exception {
        url="http://www.jokeji.cn/jokehtml/zh/2019060617150338.htm";
        Element htmlBody = JsoupUtils.getHtmlBody(url);
        Elements select = htmlBody.select("#text110");
        Elements p = select.select("p");
        List<String> strings = new LinkedList<>();
        p.stream().forEach(element ->{
            strings.add(element.html());
        });
        redisUtil.lSet("kks",strings);
        logger.info(strings.toString());
    }

    @PostMapping
    public void postContentByUrl(){
        logger.info("post is running ...");
    }
    @GetMapping("/web/{webname}")
    public void getContentByWebUrl(@PathVariable String webname) throws Exception {
        String url1="http://www.jokeji.cn/";
        Element htmlBody = JsoupUtils.getHtmlBody(url1);
        Elements select = htmlBody.select(".joketype ul li a");
        logger.info(select.toString());
    }
}
